package com.hgj.web;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

/**
 * 验证码使用
 *验证码：
 *     https://www.cnblogs.com/happyfans/p/4486010.html
 */
@WebServlet("/code.do")
public class CodeServlet extends HttpServlet {
    private static Random r = new Random();
    private static char[] chs = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".toCharArray();
    private static final int NUMBER_OF_CHS = 4;//验证码图片文字个数
    private static final int IMG_WIDTH = 65;  //验证码图片宽度
    private static final int IMG_HEIGHT = 30; //验证码图片高度

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        BufferedImage image = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, BufferedImage.TYPE_INT_RGB);    // 实例化BufferedImage
        Graphics g = image.getGraphics();
        //字体设置
        Font font = new Font("宋体", Font.BOLD, 20);
        g.setFont(font);
        //颜色设置
        Color c = new Color(200, 200, 255);  // 验证码图片的背景颜色
        g.setColor(c);
        g.fillRect(0, 0, IMG_WIDTH, IMG_HEIGHT);   // 图片的边框

        StringBuffer stringBuffer = new StringBuffer();  // 用于保存验证码字符串
        int index;       // 数组的下标
        for (int i = 0; i < NUMBER_OF_CHS; i++) {
            index = r.nextInt(chs.length); // 随机一个下标
            g.setColor(new Color(r.nextInt(88), r.nextInt(210), r.nextInt(150)));       // 随机一个颜色
            g.drawString(chs[index] + "", 15 * i + 3, 18);                              // 画出字符
            stringBuffer.append(chs[index]);                                                      // 验证码字符串
        }
        // 将验证码字符串保存到session中
        request.getSession().setAttribute("piccode", stringBuffer.toString());
        ImageIO.write(image, "jpg", response.getOutputStream());                        // 向页面输出图像
    }
}
